<?php

class Member {

//退出登录
function logout(){
	session_destroy();
	$str = array(1,"退出登录成功");
	echo json_encode($str);
}
//轮播图设置
function lbset($data){
	global $power;
	if($_SESSION['a']['alevel']!=5){
		error("权限不足");
	}
	global $conf;
	$db = new db($conf);	
	for($i=1;$i<7;$i++){
		$sy = isset($data["sy$i"])?$data["sy$i"]:"";
		$link = isset($data["link$i"])?$data["link$i"]:"";		
		$db->where("Id=$i")->update("sc_lunbo",array("pic"=>$sy,"link"=>$link));		
	}
	success("设置成功");
}
//授权登录用户个人中心
function sqlogin($data){
	global $conf;
	$db = new db($conf);
	$id = isset($data['uid'])?intval($data['uid']):0;
	$result = $db->field(array("*"))->where(array("Id"=>$id,"state"=>1))->select("sc_member");
	if($result){
		$_SESSION['userid'] = $result[0]['Id'];
		$_SESSION['tname'] = $result[0]['tname'];
		$_SESSION['uname'] = $result[0]['uname'];
		$_SESSION['state'] = $result[0]['state'];		
		$_SESSION['time'] = time();
		$str = array("1","授权成功");
		echo json_encode($str);
	}
	else{
		error("登录失败");
		echo json_encode($str);
	}
}

function gwset($data){
	global $conf;
	$db = new db($conf);
	$info = $db->field(['*'])->where("to_days(addtime) = to_days(now())")->select("sc_gwrecord");
	$fhyjcash = isset($data['cash'])?$data['cash']:0;
	$grbili = isset($data['bili'])?$data['bili']:0;
	if($info){		
		$db->where("Id=".$info[0]['Id'])->update("sc_gwrecord",['fhyjcash'=>$fhyjcash,'grbili'=>$grbili]);
	}else{
		$db->insert("sc_gwrecord",['addtime'=>date("Y-m-d H:i:s"),'fhyjcash'=>$fhyjcash,'grbili'=>$grbili]);
	}
	success('设置成功');
}

function hzlimit($data){
	global $conf;
	$db = new db($conf);
	$id = isset($data['id'])?intval($data['id']):0;
	if($id<=0){ error("操作错误");}
	$info = $db->field(array("*"))->where("Id=$id")->select("sc_member");
	if(!$info){ error('通讯异常');}
	if($info[0]['hzlimit']==1){
		$db->where("Id=$id")->update("sc_member",['hzlimit'=>0]);
		$str = [1,2,3,'否'];
	}else{
		$db->where("Id=$id")->update("sc_member",['hzlimit'=>1]);
		$str = [1,3,2,'是'];
	}
	echo json_encode($str);
}


function state($data){
	global $conf;
	$db = new db($conf);
	$id = isset($data['id'])?intval($data['id']):0;
	if($id<=0){ error("操作错误");}
	$info = $db->field(array("*"))->where("Id=$id")->select("sc_member");
	if(!$info){ error('通讯异常');}
	if($info[0]['state']==1){
		$db->where("Id=$id")->update("sc_member",['state'=>0]);
		$str = [1,2,3,'冻结'];
	}else{
		$db->where("Id=$id")->update("sc_member",['state'=>1]);
		$str = [1,3,2,'正常'];
	}
	echo json_encode($str);
}

function agentcancel($data){
	global $conf;
	$db = new db($conf);
	$id = isset($data['id'])?intval($data['id']):0;
	$dlinfo = $db->field(array("*"))->where("Id=".$id)->select("sc_agent");
	if(!$dlinfo){ error("未查询到代理信息");}
	$db->where("Id=$id")->update("sc_agent",array("state"=>0));
	$list = $db->field(array("*"))->where("uid=".$dlinfo[0]['uid'])->order("Id desc")->limit("0,1")->select("sc_agent_record");
	if($list){
		$db->where("Id=".$list[0]['Id'])->update("sc_agent_record",array("endtime"=>date("Y-m-d H:i:s")));
	}
	success("撤销成功");
}

function agentedit($data){
	global $conf;
	$db = new db($conf);
	$id = isset($data['id'])?intval($data['id']):0;
	$pro = isset($data['pro'])?intval($data['pro']):0;
	$cit = isset($data['cit'])?intval($data['cit']):0;
	$cou = isset($data['cou'])?intval($data['cou']):0;
	if($id<=0){ error("信息异常");}
	if($pro<=0){ error("请选择代理地区");}
	$dlinfo = $db->field(array("*"))->where("Id=$id and state=1")->select("sc_agent");
	if(!$dlinfo){ error("未查询到代理信息");}
	$info = $db->field(array("*"))->where("Id=".$dlinfo[0]['uid'])->select("sc_member");
	if(!$info){ error("未查询到会员信息");}
	if($cou>0){
		$level = 3;
		$agentinfo = $db->field(array("*"))->where("cou=$cou and dllevel=3 and state=1 and Id<>$id")->select("sc_agent");
	}elseif($cit>0){
		$level = 2;
		$agentinfo = $db->field(array("*"))->where("cit=$cit and dllevel=2 and state=1 and Id<>$id")->select("sc_agent");
	}else{
		$level = 1;
		$agentinfo = $db->field(array("*"))->where("pro=$pro and dllevel=1 and state=1 and Id<>$id")->select("sc_agent");
	}
	if($agentinfo){
		error("该区域已有代理 请选择其他地区");
	}
	$agentinfo = $db->field(array("*"))->where("uid=".$info[0]['Id'])->select("sc_agent");
	if($agentinfo){
		$db->where("Id=".$agentinfo[0]['Id'])->update("sc_agent",array("pro"=>$pro,"dllevel"=>$level,"cit"=>$cit,"cou"=>$cou,"state"=>1));
	}else{
		$db->insert("sc_agent",array("uid"=>$info[0]['Id'],"dllevel"=>$level,"pro"=>$pro,"cit"=>$cit,"cou"=>$cou,"state"=>1));
	}
	$db->insert("sc_agent_record",array("uid"=>$info[0]['Id'],"dllevel"=>$level,"pro"=>$pro,"cit"=>$cit,"cou"=>$cou,"addtime"=>date("Y-m-d H:i:s")));
	success("编辑成功");
}

function agentadd($data){
	global $conf;
	$db = new db($conf);
	$uname = isset($data['t0'])?trim($data['t0']):"";
	$pro = isset($data['pro'])?intval($data['pro']):0;
	$cit = isset($data['cit'])?intval($data['cit']):0;
	$cou = isset($data['cou'])?intval($data['cou']):0;
	if(!$uname){ error("请填写会员账户");}
	if($pro<=0){ error("请选择代理地区");}
	$info = $db->field(array("*"))->where("uname='".$uname."'")->select("sc_member");
	if(!$info){ error("未查询到会员信息");}
	if($cou>0){
		$level = 3;
		$agentinfo = $db->field(array("*"))->where("cou=$cou and dllevel=3 and state=1")->select("sc_agent");
	}elseif($cit>0){
		$level = 2;
		$agentinfo = $db->field(array("*"))->where("cit=$cit and dllevel=2 and state=1")->select("sc_agent");
	}else{
		$level = 1;
		$agentinfo = $db->field(array("*"))->where("pro=$pro and dllevel=1 and state=1")->select("sc_agent");
	}
	if($agentinfo){
		error("该区域已有代理 请选择其他地区");
	}
	$agentinfo = $db->field(array("*"))->where("uid=".$info[0]['Id'])->select("sc_agent");
	if($agentinfo){
		$db->where("Id=".$agentinfo[0]['Id'])->update("sc_agent",array("pro"=>$pro,"dllevel"=>$level,"cit"=>$cit,"cou"=>$cou,"state"=>1));
	}else{
		$db->insert("sc_agent",array("uid"=>$info[0]['Id'],"dllevel"=>$level,"pro"=>$pro,"cit"=>$cit,"cou"=>$cou,"state"=>1));
	}
	$db->insert("sc_agent_record",array("uid"=>$info[0]['Id'],"dllevel"=>$level,"pro"=>$pro,"cit"=>$cit,"cou"=>$cou,"addtime"=>date("Y-m-d H:i:s")));
	success("添加成功");
}

function getcit($data){
	global $conf;
	$db = new db($conf);
	$id = isset($data['id'])?intval($data['id']):0;
	$list = $db->field(array("*"))->where("areapid=$id")->select("sc_area");
	$str = "<option value='0'>--请选择--</option>";
	for($i=0;$i<count($list);$i++){
		$str = $str."<option value='".$list[$i]['areaid']."'>".$list[$i]['areaname']."</option>";
	}
	success($str);
}

function getcou($data){
	global $conf;
	$db = new db($conf);
	$id = isset($data['id'])?intval($data['id']):0;
	$list = $db->field(array("*"))->where("areapid=$id")->select("sc_area");
	$str = "<option value='0'>--请选择--</option>";
	for($i=0;$i<count($list);$i++){
		$str = $str."<option value='".$list[$i]['areaid']."'>".$list[$i]['areaname']."</option>";
	}
	success($str);
}

function czpass($data){
    global $conf;
	$db = new db($conf);
	$id = isset($data['id'])?intval($data['id']):0;
	if($id<=0){ error('通讯错误');}
	$info = $db->field(['*'])->where("Id=$id")->select("sc_cashcz");
	if(!$info){ error('未查询到充值订单');}
	if($info[0]['state']!=0){ error('已审核订单 请勿重复操作');}
	$up = $db->where("Id=$id")->update("sc_cashcz",['state'=>1,'endtime'=>date("Y-m-d H:i:s")]);
	if($up){
	    $db->where('Id='.$info[0]['uid'])->update("sc_member",['cash'=>"(cash + ".$info[0]['cash'].")"]);
	    $db->insert("sc_spend",array("uid"=>$info[0]['uid'],"cash"=>$info[0]['cash'],"type"=>10,"adda"=>1,"addtime"=>date("Y-m-d H:i:s"),"pay"=>1));
	    success('审核成功');
	}
	error('审核失败');
}

function czcancel($data){
    global $conf;
	$db = new db($conf);
	$id = isset($data['id'])?intval($data['id']):0;
	if($id<=0){ error('通讯错误');}
	$info = $db->field(['*'])->where("Id=$id")->select("sc_cashcz");
	if(!$info){ error('未查询到充值订单');}
	if($info[0]['state']!=0){ error('已审核订单 请勿重复操作');}
	$up = $db->where("Id=$id")->update("sc_cashcz",['state'=>2,'endtime'=>date("Y-m-d H:i:s")]);
	if($up){
	    success('驳回成功');
	}
	error('驳回失败');
}

function cashcz($data){
	global $conf;
	$db = new db($conf);
	$user = isset($data['t0'])?trim($data['t0']):"";
	$pwd = isset($data['t2'])?trim($data['t2']):"";
	$cash = isset($data['t1'])?(float)$data['t1']:0;
	$type = isset($data['type'])?intval($data['type']):0;
	$content = isset($data['t3'])?trim($data['t3']):"";
	if(empty($user)){error("请输入充值用户账户");}
	if(empty($pwd)){error("请输入财务密码");}
	if(empty($cash)){error("请输入充值金额");}
	if($cash>1000000){ error('单次充值金额限1000000');}
	
	if(empty($content)){
		$content = "平台充值"; 
	}
	if($type<1 || $type>2){ error('请选择充值类型');}
	$r1 = $db->field(array("*"))->where("uname='".$user."' and state=1")->select("sc_member");
	if(!$r1){error("您输入的账户未注册或已冻结");}
	$r2 = $db->field(array("count(*)"))->where("auname='".$_SESSION['a']['auname']."' and apwd2='".MD5($pwd)."'")->select("sc_admin");
	if($r2[0]['count(*)']==0){error("财务密码错误");}
	if($type==1){
		$field = array("cash"=>"(cash + $cash)");
	}else{
		$field = array("jjcash"=>"(jjcash + $cash)");
	}
		
	if($cash>0){
		$adda = 1;
		$jlcash = $cash;
	}else{
		$adda = 0;
		$jlcash = -$cash;
	}
	$cfield = array("uid"=>$r1[0]['Id'],"cash"=>$cash,"addtime"=>date("Y-m-d H:i:s",time()),"type"=>$type,"state"=>1,"content"=>$content);
	$result = $db->insert("sc_cz",$cfield);
	if($result){
		$db->where("Id=".$r1[0]['Id'])->update("sc_member",$field);	
		if($type==1){
			$db->insert("sc_spend",array("uid"=>$r1[0]['Id'],"cash"=>$jlcash,"content"=>$content,"type"=>6,"adda"=>$adda,"pay"=>1,"addtime"=>date("Y-m-d H:i:s")));	
		}else{
			$db->insert("sc_cash",array("uid"=>$r1[0]['Id'],"cash"=>$jlcash,"content"=>$content,"type"=>14,"adda"=>$adda,"addtime"=>date("Y-m-d H:i:s")));	
		}
			
		success("充值成功");
	}else{
		error("充值失败");
	}
}
function searchm($data){
	global $conf;
	$db = new db($conf);
	$info = $db->field(array("tname"))->where("uname='".$data['cardid']."'")->select("sc_member");
	if(!$info){ error("请检查会员账户");}
	success($info[0]['tname']);
}
//审核前信息确认查询
function chaxun($data){
	global $conf;
	$username = isset($data['t0'])?trim($data['t0']):"";
	if(empty($username)){
		$str = array(0,"请输入需要查询的会员名");
		echo json_encode($str);exit;
	}
	
	$db = new db($conf);
	$result = $db->field(array("*"))->where(array("uname"=>"'".$username."'"))->select("sc_member");
	if($result){
		$str = array(1,$result[0]['tname']);echo json_encode($str);
	}
	else{
		$str = array(0,"会员不存在，请查证");
		echo json_encode($str);
	}
}

function onekey($data){
	global $conf;
	$db = new db($conf);
	$field = array("state"=>1,"passtime"=>date("Y-m-d H:i:s",time()));
	$db->where(array("state"=>0))->update("sc_tixian",$field);
	$str = array(1,"已审核");
	echo json_encode($str);
}

function qrdk_no($data){
	global $conf;
	$db = new db($conf);
	$id = isset($data['uid'])?intval($data['uid']):"";
	if(empty($id)){
		$str = array(0,"系统异常，请刷新页面");
		echo json_encode($str);exit;
	}
	$txinfo = $db->field(array("*"))->where(array("Id"=>$id))->select("sc_tixian");
	if($txinfo){
		if(strlen($id)!=strlen($data['uid'])){
			$str = array(0,"系统异常，请刷新页面");
			echo json_encode($str);exit;
		}
		if($txinfo[0]['state']==1){
			$str = array(0,"已审核打款，请勿重复操作");
			echo json_encode($str);exit;
		}
		$type = $txinfo[0]['type'];
		$db->where("Id=$id and state=0")->update("sc_tixian",array("state"=>2,"passtime"=>date("Y-m-d H:i:s")));
		
		$db->where("Id=".$txinfo[0]['uid'])->update("sc_member",array("jjcash"=>"(jjcash + ".$txinfo[0]['num'].")"));
		$db->insert("sc_cash",array("uid"=>$txinfo[0]['uid'],"cash"=>$txinfo[0]['num'],"content"=>"提现驳回","addtime"=>date("Y-m-d H:i:s"),"type"=>12,"adda"=>1));		
		$str = array(1,"已驳回");
		echo json_encode($str);
	}
	else{
		$str = array(0,"系统异常，请刷新页面");
		echo json_encode($str);exit;
	}
}

function qrdk($data){
	global $conf;
	$db = new db($conf);
	$id = isset($data['uid'])?intval($data['uid']):"";
	if(empty($id)){
		$str = array(0,"系统异常，请刷新页面");
		echo json_encode($str);exit;
	}
	$result = $db->field(array("*"))->where(array("Id"=>$id))->select("sc_tixian");
	if($result){
		if(strlen($id)!=strlen($data['uid'])){
			$str = array(0,"系统异常，请刷新页面");
			echo json_encode($str);exit;
		}
		if($result[0]['state']==1){
			$str = array(0,"已审核打款，请勿重复操作");
			echo json_encode($str);exit;
		}
		$field = array("state"=>1,"passtime"=>date("Y-m-d H:i:s",time()));
		$db->where(array("Id"=>$id,"state"=>0))->update("sc_tixian",$field);
		$str = array(1,"已审核");
		echo json_encode($str);		
	}
	else{
		$str = array(0,"系统异常，请刷新页面");
		echo json_encode($str);exit;
	}
}

	public function uedit($data){
		global $conf;
		$db = new db($conf);
		if(count($data)<=0){
			$str = array(0,"信息已更新，请刷新页面");
			echo json_encode($str);exit;
		}
		$userid = isset($data['id'])?$data['id']:"";
		$username = isset($data['t0'])?htmlspecialchars($data['t0']):"";
		$phone = isset($data['phone'])?htmlspecialchars($data['phone']):"";
		$password = isset($data['t1'])?$data['t1']:"";
		$truename = isset($data['t2'])?htmlspecialchars($data['t2']):"";
		$level = isset($data['level'])?intval($data['level']):0;		
		$tlevel = isset($data['tlevel'])?intval($data['tlevel']):0;
		$hx = isset($data['hx'])?intval($data['hx']):0;
			
		if(empty($userid)){$str = array(0,"信息异常，请刷新当前页面");	echo json_encode($str);exit;}
		if(strlen($phone)!=11){$str = array(0,"请输入会员11位手机号");	echo json_encode($str);exit;}
		if(empty($truename)){$str = array(0,"请输入会员真实姓名");	echo json_encode($str);exit;}		
		if($level>2 || $level<0){ error("请选择团队等级");}	
		if($tlevel>7 || $tlevel<0){ error("请选择团队等级");}	
		$info = $db->field(array("*"))->where("Id=$userid")->select("sc_member");
		if($tlevel>0){
		    $level = 2;
		}
		$field = array("tname"=>$truename,"phone"=>$phone,"level"=>$level,"tlevel"=>$tlevel,"hx"=>$hx);
		
		$set = zdset();
		if(!empty($password)){			
			$field['pwd1']=MD5($password);
			$field['pwd2']=MD5($password);
		}		
		
		$up = $db->where(array("Id"=>$userid,"uname"=>"'".$username."'"))->update("sc_member",$field);
		if($up){			
			if($level!=$info[0]['level']){
				$db->insert("sc_uplevel",array("uid"=>$info[0]['Id'],"levela"=>$info[0]['level'],"levelb"=>$level,"content"=>"系统调整","addtime"=>date("Y-m-d H:i:s"),"type"=>1,"xt"=>1,"state"=>1));
			}
			if($tlevel!=$info[0]['tlevel']){
				$db->insert("sc_uplevel",array("uid"=>$info[0]['Id'],"levela"=>$info[0]['tlevel'],"levelb"=>$tlevel,"content"=>"系统调整","addtime"=>date("Y-m-d H:i:s"),"type"=>1,"xt"=>1,"lx"=>2,"state"=>1));
			}
			if($level>0){
				if($info[0]['level']==0){					
					$tjinfo = $db->field(array("*"))->where("Id=".$info[0]['tjid']." and level>0")->select("sc_member");
					if($tjinfo){
						if($tjinfo[0]['level']>1){
							$lbid = $tjinfo[0]['Id'];
							$pxnum = 0;
						}else{
							$lbid = $tjinfo[0]['lbid'];
							$pxnum = 1;
						}												
						$db->where("Id=".$info[0]['Id'])->update("sc_member",array("lbid"=>$lbid,"pxnum"=>$pxnum));
						if($tjinfo[0]['level']==1){
							$ztnum = $db->field(['count(*)'])->where('level>0 and state=1 and tjid='.$tjinfo[0]['Id'])->select("sc_member");
							if($ztnum[0]['count(*)']>1){
								$db->where("Id=".$tjinfo[0]['Id'])->update("sc_member",array("level"=>2));													
								$db->insert("sc_uplevel",array("uid"=>$tjinfo[0]['Id'],"levela"=>1,"levelb"=>2,"addtime"=>date("Y-m-d H:i:s"),"type"=>2,"lx"=>1,"content"=>"直推会员升级","state"=>1));	
							}					
						}						
					}

					$ztnum = $db->field(['count(*)'])->where('level>0 and state=1 and tjid='.$info[0]['Id'])->select("sc_member");
					if($ztnum[0]['count(*)']>1){
						$db->where("Id=".$info[0]['Id'])->update("sc_member",array("level"=>2));													
						$db->insert("sc_uplevel",array("uid"=>$info[0]['Id'],"levela"=>1,"levelb"=>2,"addtime"=>date("Y-m-d H:i:s"),"type"=>2,"lx"=>1,"content"=>"直推会员升级","state"=>1));	
					}
					$db->where("Id in(".$info[0]['jiedian'].")")->update("sc_member",['onum'=>"(onum + 1)"]);
				}
			}else{
				if($info[0]['level']>0){
					$db->where("Id in(".$info[0]['jiedian'].")")->update("sc_member",['onum'=>"(onum - 1)"]);
				}
			}
			
			$jiedian = $info[0]['Id'].",".$info[0]['jiedian'];
			if(($level==2 && $info[0]['level']!=$level) || $tlevel>$info[0]['tlevel']){//用户成为分公司或者股东级别上调触发团队升级				
				$db->where("Id in($jiedian) and tdlevel<$tlevel")->update("sc_member",['tdlevel'=>$tlevel]);//更新团队股东等级
				$jiedian = explode(',',$info[0]['Id'].",".$info[0]['jiedian']);
				for($i=0;$i<count($jiedian)-1;$i++){
					$tdinfo = $db->field(['*'])->where("Id = ".$jiedian[$i]." and tlevel<7 and level=2 and state=1")->select("sc_member");
					if($tdinfo){						
						$tdlevel = $tdinfo[0]['tlevel'];											
						for($n=$tdlevel;$n<7;$n++){
							if($n==0){
								$tjlist = $db->field(['*'])->where('level=2 and state=1 and tjid='.$tdinfo[0]['Id'])->order('onum desc')->select("sc_member");
								if(count($tjlist)>=$set['td11'] && (($set['td12']>0 && $tjlist[$set['td12']-1]['onum']>=$set['td13']) || $set['td12']==0 || $set['td13']==0)){
									$tdlevel = 1;
								}else{
									break;
								}
							}elseif($n<3){
								$tjlist = $db->field(['count(*)'])->where("tlevel>=$n and state=1 and tjid=".$tdinfo[0]['Id'])->select("sc_member");
								if($tjlist[0]['count(*)']>=$set['td'.($n+1)]){
									$tdlevel = $n + 1;
								}else{
									break;
								}
							}else{
								$tjlist = $db->field(['count(*)'])->where("tdlevel>=$n and state=1 and tjid=".$tdinfo[0]['Id'])->select("sc_member");
								if($tjlist[0]['count(*)']>=$set['td'.($n+1)]){
									$tdlevel = $n + 1;
								}else{
									break;
								}
							}
						}														
						if($tdlevel>$tdinfo[0]['tlevel']){
							$db->where("Id=".$tdinfo[0]['Id'])->update("sc_member",['tlevel'=>$tdlevel]);
							$db->insert("sc_uplevel",array("uid"=>$tdinfo[0]['Id'],"levela"=>$tdinfo[0]['tlevel'],"levelb"=>$tdlevel,"addtime"=>date("Y-m-d H:i:s"),"type"=>2,"content"=>"业绩达标","lx"=>2,"state"=>1));
							$db->where("Id in(".$tdinfo[0]['Id'].",".$tdinfo[0]['jiedian'].") and tdlevel<$tdlevel")->update("sc_member",['tdlevel'=>$tdlevel]);
						}						
					}	
				}		
			}elseif($tlevel<$info[0]['tlevel']){
				$jiedian = explode(',',$info[0]['Id'].",".$info[0]['jiedian']);
				for($i=0;$i<count($jiedian)-1;$i++){
					$tdinfo = $db->field(['*'])->where("Id = ".$jiedian[$i])->select("sc_member");
					if($tdinfo[0]['tdlevel']<=$info[0]['tlevel']){
						$tjlist = $db->field(['Id','tdlevel'])->where("tjid=".$tdinfo[0]['Id'])->order("tdlevel desc")->select("sc_member");
						if($tjlist){
							$tdlevel = $tjlist[0]['tdlevel']>$tlevel?$tjlist[0]['tdlevel']:$tlevel;
							$tdlevel = $tdlevel>$tdinfo[0]['tlevel']?$tdlevel:$tdinfo[0]['tlevel'];
							$db->where("Id=".$tdinfo[0]['Id'])->update("sc_member",['tdlevel'=>$tdlevel]);
							if($tdlevel>=$info[0]['tlevel']){
								break;
							}
						}else{
							$db->where("Id=".$tdinfo[0]['Id'])->update("sc_member",['tdlevel'=>$tlevel]);
						}											
					}else{
						break;
					}				
				}
			}
			
			
			$str = array(1,"修改成功",1);			
			echo json_encode($str);
		}else{
			$str = array(1,"修改成功");	echo json_encode($str);
		}		
	}

	function uban($data){
		global $conf;
		$db = new db($conf);
		if(count($data)<=0){
			$str = array(0,"信息已更新，请刷新页面");
			echo json_encode($str);exit;
		}
		$id = $data['id'];
		$result = $db->field(array("uname","state"))->where(array("Id"=>$id))->select("sc_member");
		if(!$result) { $str = array(0,"会员不存在"); echo json_encode($str);exit;}
		if($result[0]['state']==0){ $str = array(0,"已冻结账户"); echo json_encode($str);exit;}
		$field = array("state"=>0);
		$result1 = $db->where(array("Id"=>$id))->update("sc_member",$field);
		if($result){
			$str = array("1","帐号已冻结");
			echo json_encode($str);
		}
		else{
			$str = array("0","冻结帐号失败");
			echo json_encode($str);
		}	
	}

	function unban($data){
		global $conf;
		$db = new db($conf);
		if(count($data)<=0){
			$str = array(0,"信息已更新，请刷新页面");
			echo json_encode($str);exit;
		}
		$id = $data['id'];
		$result = $db->field(array("uname","state"))->where(array("Id"=>$id))->select("sc_member");
		if(!$result) { $str = array(0,"会员不存在"); echo json_encode($str);exit;}
		if($result[0]['state']==1){ $str = array(0,"未冻结账户"); echo json_encode($str);exit;}
		$field = array("state"=>1);
		$result1 = $db->where(array("Id"=>$id))->update("sc_member",$field);
		if($result){
			$str = array("1","帐号已解冻");
			echo json_encode($str);
		}
		else{
			$str = array("0","解冻帐号失败");
			echo json_encode($str);
		}	
	}

	function huifu($data){
		global $conf;
		$db = new db($conf);
		if(count($data)<=0){
			$str = array(0,"信息已更新，请刷新页面");
			echo json_encode($str);exit;
		}
		$replay = isset($data['t0'])?$data['t0']:"";
		$mid = isset($data['mid'])?$data['mid']:"";
		if(!$replay){
			$str = array("0","请输入回复内容"); echo json_encode($str);exit;
		}
		if(empty($mid)){
			$str = array("0","操作异常，请刷新页面"); echo json_encode($str);exit;
		}
		$field = array("replay"=>$replay,"replaytime"=>date("Y-m-d H:i:s",time()),"state"=>1);
		$result = $db->where(array("Id"=>$mid,"state"=>0))->update("sc_message",$field);
		if($result){
			$str = array(1,"回复成功"); echo json_encode($str);
		}
		else{
			$str = array(0,"回复失败，请刷新重试"); echo json_encode($str);
		}
		
	}

	function mdel($data){
		global $conf;
		$db = new db($conf);
		$id = isset($data['id'])?intval($data['id']):0;
		if(!$id){ error("通信异常");}
		$del = $db->where("Id=$id")->delete("sc_message");
		if($del){success("删除成功");}else{ error("删除失败");}
	}

function medit($data){
	global $conf;
	$db = new db($conf);
	$uname = isset($data['t1'])?trim($data['t1']):"";
	$upswd = isset($data['t2'])?trim($data['t2']):"";
	$utname = isset($data['t3'])?trim($data['t3']):"";
	if(!$uname){error("账户信息异常");}	
	if(!$utname){error("给管理员起个绰号");}
	$field = array("atname"=>$utname);
	if($upswd){
		$field['apwd']=MD5($upswd);
		$field['apwd2']=MD5($upswd);	
	}
	$r1 = $db->where(array("auname"=>"'".$uname."'"))->update("sc_admin",$field);
	success("保存成功");
	
}

function onek($data){
	global $conf;
	$db = new db($conf);
	$field = array("state"=>1,"passtime"=>date("Y-m-d H:i:s",time()));
	$db->where(array("state"=>0))->update("sc_tixian",$field);
	$str = array(1,"已审核");
	echo json_encode($str);
}

function powerset($data){
	global $conf;
	$db = new db($conf);
	$uid = isset($data['uid'])?$data['uid']:0;
	if(!$uid){error("通信异常");}

	$tjsp = isset($data["tjsp"])?intval($data['tjsp']):0;
	$spfl = isset($data["spfl"])?intval($data['spfl']):0;	
	if($tjsp || $spfl){ $spgl = 1;}else{ $spgl=0;}	
	$scdd = isset($data["scdd"])?intval($data['scdd']):0;

	$hylb = isset($data["hylb"])?intval($data['hylb']):0;
	$lylb = isset($data["lylb"])?intval($data['lylb']):0;
	if($hylb || $lylb){ $hygl = 1;}else{ $hygl=0;}
	
	$qydl = isset($data["qydl"])?intval($data['qydl']):0;

    $mrtj = isset($data["mrtj"])?intval($data['mrtj']):0;
    $cwjl = isset($data["cwjl"])?intval($data['cwjl']):0;
	$jjjl = isset($data["jjjl"])?intval($data['jjjl']):0;
	$nlzjl = isset($data["nlzjl"])?intval($data['nlzjl']):0;
	$gdfh = isset($data["gdfh"])?intval($data['gdfh']):0;
	$edff = isset($data["edff"])?intval($data['edff']):0;
	$gwz = isset($data["gwz"])?intval($data['gwz']):0;
	$txjl = isset($data["txjl"])?intval($data['txjl']):0;
	$hzjl = isset($data["hzjl"])?intval($data['hzjl']):0;
	$hycz = isset($data["hycz"])?intval($data['hycz']):0;
	$xxcz = isset($data["xxcz"])?intval($data['xxcz']):0;
	if($mrtj || $cwjl || $jjjl || $nlzjl || $gdfh || $edff || $gwz || $txjl || $hzjl || $hycz || $xxcz){ $cwgl = 1;}else{ $cwgl=0;}	
	$gggl = isset($data["gggl"])?intval($data['gggl']):0;
	$lbtsz = isset($data["lbtsz"])?intval($data['lbtsz']):0;
	
	$field = array("tjsp"=>$tjsp,"spfl"=>$spfl,"spgl"=>$spgl,"scdd"=>$scdd,"hylb"=>$hylb,"lylb"=>$lylb,"hygl"=>$hygl,"qydl"=>$qydl,"mrtj"=>$mrtj,"cwjl"=>$cwjl,"jjjl"=>$jjjl,"nlzjl"=>$nlzjl,"gdfh"=>$gdfh,"edff"=>$edff,"gwz"=>$gwz,"txjl"=>$txjl,"hzjl"=>$hzjl,"hycz"=>$hycz,"xxcz"=>$xxcz,"cwgl"=>$cwgl,"gggl"=>$gggl,"lbtsz"=>$lbtsz);
	
	$arr = json_encode($field);
	$db->where("uid=$uid")->update("sc_power",array("power"=>$arr));

	success("保存成功"); 
	
}

function madd($data){
	global $conf;
	$db = new db($conf);
	$uname = isset($data['t1'])?trim($data['t1']):"";
	$upswd = isset($data['t2'])?trim($data['t2']):"";
	$utname = isset($data['t3'])?trim($data['t3']):"";
	if(!$uname){error("请输入管理员账户");}
	if(!$upswd){error("请输入管理员密码");}
	if(!$utname){error("给管理员起个绰号");}
	$r1 = $db->field(array("count(*)"))->where(array("auname"=>"'".$uname."'"))->select("sc_admin");
	if($r1[0]['count(*)']>0){error("已存在相同用户名");}
	$field = array("auname"=>$uname,"atname"=>$utname,"apwd"=>MD5($upswd),"apwd2"=>MD5($upswd),"alevel"=>3,"astate"=>1);
	$db->insert("sc_admin",$field);
	$id = $db->done("LAST_INSERT_ID()");
	if($id){
		$arr = array("tjsp"=>0,"spfl"=>0,"spgl"=>0,"scdd"=>0,"hylb"=>0,"lylb"=>0,"hygl"=>0,"qydl"=>0,"mrtj"=>0,"cwjl"=>0,"jjjl"=>0,"nlzjl"=>0,"gdfh"=>0,"edff"=>0,"gwz"=>0,"txjl"=>0,"hzjl"=>0,"hycz"=>0,"xxcz"=>0,"cwgl"=>0,"gggl"=>0,"lbtsz"=>0);
		$power = json_encode($arr);
		$db->insert("sc_power",array("uid"=>$id[0]['LAST_INSERT_ID()'],"power"=>$power));
		success("添加成功");
	}else{
		error("添加失败");
	}
	
}

		function xgpassword(){
			global $conf;
			$db = new db($conf);
			$username = $_SESSION['a']['auname'];
			$oldpass = isset($_POST['t0'])?trim($_POST['t0']):"";
			$newpass = isset($_POST['t1'])?trim($_POST['t1']):"";
			$newpass1 = isset($_POST['t2'])?trim($_POST['t2']):"";
			if(empty($oldpass)){error("请输入原密码");}
			if(empty($newpass)){error("请输入新密码");}
			if(empty($newpass1)){error("请再次输入新密码");}
			if(strlen($newpass)<6){error("请输入6位以上字母+数字密码");}
			if((strlen($newpass) != strlen($newpass1)) || $newpass != $newpass1){error("重复输入密码不一致");}
			$result = $db->field(array("auname","apwd"))->where(array("auname"=>"'".$username."'","apwd"=>"'".MD5($oldpass)."'"))->select("sc_admin");
			if($result){
				if(($result[0]['auname']!=$username) || strlen($result[0]['auname'])!=strlen($username)){
					error("修改密码失败");				
				}
				else{
					$field = array("apwd"=>MD5($newpass));
					$r1 = $db->where(array("auname"=>"'".$result[0]['auname']."'"))->update("sc_admin",$field);
					if(!$r1){error("修改密码失败");}
					else{success("修改成功");}
				}
			}
			else{error("原密码错误");}
		}
	
		function xgpwdzf(){
			global $conf;
			$db = new db($conf);
			$username = $_SESSION['a']['auname'];
			$oldpass = isset($_POST['t0'])?trim($_POST['t0']):"";
			$newpass = isset($_POST['t1'])?trim($_POST['t1']):"";
			$newpass1 = isset($_POST['t2'])?trim($_POST['t2']):"";
			if(empty($oldpass)){error("请输入原支付密码");}
			if(empty($newpass)){error("请输入新支付密码");}
			if(empty($newpass1)){error("请再次输入新支付密码");}
			if(strlen($newpass)<6){error("请输入6位以上字母+数字密码");}
			if((strlen($newpass) != strlen($newpass1)) || $newpass != $newpass1){error("重复输入密码不一致");}
			$result = $db->field(array("auname","apwd2"))->where(array("auname"=>"'".$username."'","apwd2"=>"'".MD5($oldpass)."'"))->select("sc_admin");
			if($result){
				if(($result[0]['auname']!=$username) || strlen($result[0]['auname'])!=strlen($username)){
					error("修改密码失败");				
				}
				else{
					$field = array("apwd2"=>MD5($newpass));
					$r1 = $db->where(array("auname"=>"'".$result[0]['auname']."'"))->update("sc_admin",$field);
					if(!$r1){error("修改密码失败");}
					else{success("修改成功");}
				}
			}
			else{error("原支付密码错误");}
		}
	
}